/*登录模块*/
#include "head.h"

/*拿到连接上的客户端的地址信息，为登录做准备*/
user enter(int sockfd)
{
    int a = 0;
    requst buf = {0};
    user use;
    struct sockaddr_in peer = {0};
    int len = sizeof(struct sockaddr_in);
    getpeername(sockfd, (struct sockaddr *)&peer, &len); // 拿到对方的地址信息

    
    int ret;
    if (ret = recv(sockfd, &buf, sizeof(requst), 0) == 0)
    {
        printf("[%s:%d]断开连接\n", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
        user err;
        err.dl = 5;
        err.id = 0;
        return err;
    }
    else if (ret < 0)
    {
        perror("recv");
        close(sockfd);
    }

    a = doserver(sockfd, (void *)&buf);
    if (a == 0)
    {
        printf("%s 登录\n", buf.name);
    }
    use.id = buf.id;
    use.dl = a;
    return use;
}

/*登录选项*/
int doserver(int sockc, void *buf)
{
    requst *q = (requst *)buf;
    switch (q->option)
    {
    case 1:
        sign_in(sockc, q);
        break;
    case 2:
        return log_in(sockc, q);
    }
    return 3;
}

/*登录模块*/
int log_in(int sockc, requst *p)
{
    int success = 0;
    char buf[2] = {0};
    char const *q[] = {"登录成功", "登录失败"};

    success = info(p); // mysql注册模块
    send(sockc, q[success], strlen(q[success]) + 1, 0);
    recv(sockc, buf, 2, 0);
    send(sockc, p->name, 12, 0);
    return success;
}

/*注册模块*/
int sign_in(int sockc, requst *p)
{
    requst w = *p;
    int success = 0;
    char const *q[] = {"注册成功", "注册失败"};

    printf("注册\n");
    success = info_2(w); // mysql注册模块
    send(sockc, q[success], strlen(q[success]) + 1, 0);
    return 0;
}